package com.archerdu.apply.service.impl;

import java.util.List;

import com.archerdu.apply.dao.MemberDAO;
import com.archerdu.apply.model.Member;
import com.archerdu.apply.service.MemberManager;
import com.archerdu.base.dao.IBaseDao;
import com.archerdu.base.exception.BussinessException;
import com.archerdu.base.service.BaseManager;
import com.archerdu.commons.constant.ErrorConstant;
import com.archerdu.commons.util.SHA256;

public class MemberManagerImpl extends BaseManager<Member, Integer> implements MemberManager {

    private static final long serialVersionUID = 9163067090144196614L;

    private MemberDAO           memberDao;

    public MemberDAO getMemberDao() {

        return memberDao;
    }

    @Override
    public void add(Member entity) throws Exception {
        // SHA256加密密码
        Member member = new Member();
        member.setLoginname(entity.getLoginname());
        if (getAll(member).size() == 0) {
            entity.setPwd(SHA256.encrypt(entity.getPwd()));
            super.add(entity);
        } else {
            throw new BussinessException(ErrorConstant.USER_AREADY_EXISTS);
        }
    }

    public void setMemberDao(MemberDAO dao) {

        memberDao = dao;
    }

    @Override
    protected IBaseDao<Member, Integer> getDao() {

        return (IBaseDao<Member, Integer>) memberDao;
    }

    @Override
    public Member checkLogin(Member member) {
        member.setPwd(SHA256.encrypt(member.getPwd()));
        List<Member> list = getDao().findAll(member);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }
}
